package com.jzo2o.market.handler;

import cn.hutool.core.collection.CollUtil;
import com.jzo2o.common.utils.DateUtils;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.redis.sync.SyncManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class XxlJobHandler {

    @Resource
    private SyncManager syncManager;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponService couponService;

    /**
     * 活动状态修改，
     * 1.活动进行中状态修改
     * 2.活动已失效状态修改
     * 1分钟一次
     */
    @XxlJob("updateActivityStatus")
    public void updateActivityStatus(){
        List<Activity> list = activityService.lambdaQuery()
                .notIn(Activity::getStatus, ActivityStatusEnum.LOSE_EFFICACY.getStatus(), ActivityStatusEnum.VOIDED.getStatus())
                .list();

        if(CollUtil.isEmpty(list)){
            return;
        }

        for (Activity activity : list) {
            if(activity.getStatus() == ActivityStatusEnum.NO_DISTRIBUTE.getStatus() && activity.getDistributeStartTime().isBefore(DateUtils.now())){
                activityService.lambdaUpdate()
                        .eq(Activity::getId, activity.getId())
                        .set(Activity::getStatus, ActivityStatusEnum.DISTRIBUTING.getStatus())
                        .update();
            }

            if(activity.getDistributeEndTime().isBefore(DateUtils.now())){
                activityService.lambdaUpdate()
                        .eq(Activity::getId, activity.getId())
                        .set(Activity::getStatus, ActivityStatusEnum.VOIDED.getStatus())
                        .update();
            }
        }
    }

    /**
     * 已领取优惠券自动过期任务
     */
    @XxlJob("processExpireCoupon")
    public void processExpireCoupon() {
        List<Coupon> list = couponService.lambdaQuery()
                .eq(Coupon::getStatus, CouponStatusEnum.NO_USE.getStatus())
                .list();

        if(CollUtil.isEmpty(list)){
            return;
        }

        for (Coupon coupon : list) {
            if(coupon.getValidityTime().isBefore(DateUtils.now())){
                couponService.lambdaUpdate()
                        .eq(Coupon::getId, coupon.getId())
                        .set(Coupon::getStatus, CouponStatusEnum.INVALID.getStatus())
                        .update();
            }
        }
    }

    /**
     * 已领取优惠券自动过期任务
     */
    @XxlJob("activityPreheat")
    public void activityPreheat() {
        log.info("优惠券活动定时预热...");
        try {
            activityService.preheat();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
